package com.cn.algorithm.two.line.test;

import java.util.Arrays;

public class InsertSortJ {
    public static void main(String[] args) {
        int[] arr = {9, 7, 4, 2, 5, 3, 8};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            int k = i;
            while (k >= 1) {
                if (arr[k] < arr[k - 1]) {
                    swap(arr, k, k - 1);
                    k--;
                } else {
                    break;
                }
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
